import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


import java.awt.Color;


import java.awt.Font;

import java.awt.Polygon;
import java.io.PrintStream;

import java.util.Locale;
import java.util.Random; 



import pluginUtils.Circle;
import pluginUtils.CircleArrangement;
import pluginUtils.Complexe; 
import pluginUtils.DoyleCircle;
import pluginUtils.MobiusTransform;
import pluginUtils.CircleInversion;
import pluginUtils.PackCircle;
import pluginUtils.Point2D;
import pluginUtils.Pos3D;
import pluginUtils.Sphere;
import pluginUtils.SphereInversion;

// La version actuelle est #360

public class GenericDrawerAngle extends ImagePlus implements PlugIn{
	{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=2000,Y=2000; // image size
	
	private PrintStream output;
	private double xShift=0; 
	private double zShift=0; 
	
	

	private Random generator=new Random(); // One always needs some randomness
	
	// For the scaling of the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 
	
	private String[] palette={"Yellow","Red","Blue","Green","White","NavyBlue","Turquoise","Orange"};
	private Color[] palette1={Color.YELLOW,Color.RED,Color.BLUE,Color.GREEN,Color.MAGENTA,Color.ORANGE};
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 
	
	
	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	
	
	private void drawCircle(Circle c){
		double xCenter=c.getXcenter(); 
		double yCenter=c.getYcenter(); 
		
		int intX=(int)((xCenter-xShift)*acoef+ccoef);
		int intY=(int)((yCenter-zShift)*vcoef+wcoef);
		int intRx=(int)(c.getRadius()*acoef);
		int intRy=(int)(c.getRadius()*vcoef);
		primeIP.drawOval(intX-intRx,intY-intRy,intRx*2,intRy*2);
	}
	
	private void drawPoint(double x,double y){
		int radi=15; 
		int intX=(int)(x*acoef+ccoef);
		int intY=(int)(y*vcoef+wcoef);
		primeIP.fillOval(intX-radi,intY-radi,2*radi,2*radi);
	}
	
	private void drawPoint(Point2D p){
		int intX=(int)(p.getX()*acoef+ccoef);
		int intY=(int)(p.getY()*vcoef+wcoef);
		primeIP.fillOval(intX-10,intY-10,20,20);
	}
	
	
	private void drawTriangle(double[] X,double[] Y){
		int xint[]=new int[3]; 
		int yint[]=new int[3];
		for(int i=0;i<3;i++){
			xint[i]=(int)(X[i]*acoef+ccoef); 
			yint[i]=(int)(Y[i]*vcoef+wcoef);
		}
		primeIP.fillPolygon(new Polygon(xint,yint,3)); 
		
	}
	
	
	
	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
	private void drawLine(Point2D p,Point2D q){
		int XD=(int)(p.getX()*acoef+ccoef); 
		int YD=(int)(p.getY()*vcoef+wcoef); 
		int XF=(int)(q.getX()*acoef+ccoef); 
		int YF=(int)(q.getY()*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	public double f(double x,double p,double l){
		return p*(x*x/(l*l)-1); 
	}
	
	public double choc(double x,double p,double l,double u){
		return x*((p*u)/(l*l)-(l*l)/(4*p*u))+(l*l)/(4*p)-p; 
	}
	
	
	public void run(String arg) {
		primeIP = new ColorProcessor(X,Y);
		
		
		
		
	
	
		
	
		double limit=10; 
		double lmoins=-10;
		
		setBounds(lmoins,limit,lmoins,limit);
		//setBounds(-0.25,0.5,-0.25,0.5); 
		setCoefs(); 
		
		
		
		
		
		
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.WHITE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(5); 
		

		setProcessor("Verifying", primeIP);
		
	
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(1);
		
		drawCircle(new Circle(Point2D.ZERO,8)); 
		primeIP.setColor(Color.GREEN);
		primeIP.setLineWidth(1);
		drawLine(Point2D.ZERO,new Point2D(limit,0));
		primeIP.setLineWidth(1);
		primeIP.setColor(Color.BLACK);
		
		/* B et L 
		double angleNoir[]={14.307,36.92,62.04,91.329,126.06,165.84,207.26,245.65,278.57,306.349,330.49,352.64}; 
		double angleRouge[]={7.35,29.5,53.65,81.42,114.35,152.73,194.15,233.93,268.67,297.95,323.07,345.69};
		*/
		/*A 
		double angleNoir[]={0,22.58,46.56,73.39,104.45,140.45,180,360-22.48,360-46.56,360-73.39,360-104.45,360-140.45}; 
		double angleRouge[]={0,22.58,46.56,73.39,104.45,140.45,180,360-22.48,360-46.56,360-73.39,360-104.45,360-140.45}; 
		*/
		/* C et K
		double angleNoir[]={0,19.89,41.409,66.42,97.18,135.48,180,360-19.89,360-41.409,360-66.42,360-97.18,360-135.48}; 
		double angleRouge[]={0,19.89,41.409,66.42,97.18,135.48,180,360-19.89,360-41.409,360-66.42,360-97.18,360-135.48};
		 */
		/* D et J 
		double angleNoir[]={0,17.58,36.689,60,90,130.2,180,360-17.58,360-36.869,360-60,360-90,360-130.20}; 
		double angleRouge[]={0,17.58,36.689,60,90,130.2,180,360-17.58,360-36.869,360-60,360-90,360-130.20};
		*/
		/* E et I
		double angleNoir[]={11.89,28.566,48.599,75.277,113.578,166.55,223.78,268.95,300.27,322.83,340.73,356.498}; 
		double angleRouge[]={3.5,19.26,37.16,59.72,91.04,136.21,193.44,246.92,284.72,311,4,331.43,348.10}; 
		*/
		
		/* F et H
		double angleNoir[]={5.76,20.38,37.47,59.91,92.839,143.07,206.41,259.88,295.33,319.14,336.9,351.8}; 
		double angleRouge[]={8.19,23.09,40.85,64.66,100.11,153.58,216.92,267.76,300.08,322.52,339.61,354.23}; 
		*/
		// G
		double angleNoir[]={0,13.66,28.95,48.189,75.52,118.14,180,360-13.66,360-28.95,360-48.189,360-75.52,360-118.14}; 
		double angleRouge[]={0,13.66,28.95,48.189,75.52,118.14,180,360-13.66,360-28.95,360-48.189,360-75.52,360-118.14}; 
		
		for(int i=0;i<12;i++){
			double angleRad=angleNoir[i]*Math.PI/180;
			if(angleRad>=3*Math.PI/2)
				angleRad=angleRad-2*Math.PI; 
			int hau=(int)(limit*Math.tan(angleRad)); 
			if (Math.abs(angleRad)<=Math.PI/2){
				drawLine(Point2D.ZERO,new Point2D(limit,-hau));
				System.out.println(angleNoir[i]); 
			}
			else{
				drawLine(Point2D.ZERO,new Point2D(-limit,hau));
				System.out.println("** "+angleNoir[i]); 
			}
			
		}
		primeIP.setColor(Color.RED);
		primeIP.setLineWidth(1);
		for(int i=0;i<12;i++){
			double angleRad=angleRouge[i]*Math.PI/180;
			if(angleRad>=3*Math.PI/2)
				angleRad=angleRad-2*Math.PI; 
			int hau=(int)(limit*Math.tan(angleRad)); 
			if (Math.abs(angleRad)<=Math.PI/2){
				drawLine(Point2D.ZERO,new Point2D(limit,-hau));
				System.out.println(angleRouge[i]); 
			}
			else{
				drawLine(Point2D.ZERO,new Point2D(-limit,hau));
				System.out.println("** "+angleRouge[i]); 
			}
			
		}
		show();
	}	
}
